package com.jianghu;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;

public class MainPage extends Activity {

	private SharedPreferences sp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainpage);
		GridView gridview = (GridView) findViewById(R.id.gridviewofMainPage);
		ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
		for (int i = 1; i < 10; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			if (i == 1) {
				map.put("ItemImage", R.drawable.recommend);
				map.put("ItemText", getResources()
						.getString(R.string.recommend));
			}
			if (i == 2) {
				map.put("ItemImage", R.drawable.hottopic);
				map.put("ItemText", getResources().getString(R.string.hottopic));
			}
			if (i == 3) {
				map.put("ItemImage", R.drawable.myrecommend);
				map.put("ItemText",
						getResources().getString(R.string.myrecommend));
			}
			if (i == 4) {
				map.put("ItemImage", R.drawable.frinedtrends);
				map.put("ItemText",
						getResources().getString(R.string.friendtrends));
			}
			if (i == 5) {
				map.put("ItemImage", R.drawable.mytrends);
				map.put("ItemText", getResources().getString(R.string.mytrends));
			}
			if (i == 6) {
				map.put("ItemImage", R.drawable.messagecenter);
				map.put("ItemText",
						getResources().getString(R.string.messagecenter));
			}
			if (i == 9) {
				map.put("ItemImage", R.drawable.configuration);
				map.put("ItemText",
						getResources().getString(R.string.configuration));
			}
			lstImageItem.add(map);

		}
		SimpleAdapter saImageItems = new SimpleAdapter(this, lstImageItem,
				R.layout.grid_item, new String[] { "ItemImage", "ItemText" },
				new int[] { R.id.ItemImage, R.id.ItemText });

		gridview.setAdapter(saImageItems);
		gridview.setOnItemClickListener(new ItemClickListener());

		ImageButton imageLogout = (ImageButton) findViewById(R.id.logout);
		imageLogout.setOnClickListener(new imageLogoutClickListener());

	}

	class imageLogoutClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			new AlertDialog.Builder(MainPage.this)
					.setItems(R.array.items,
							new DialogInterface.OnClickListener() { // content
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									if (which == 0) {
										Intent intent = new Intent();
										intent.setClass(MainPage.this,
												FirstPage.class);
										intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
										intent.putExtra("close", "close");
										startActivity(intent);
									} else {
										sp = getSharedPreferences("UserInfo", 0);
										sp.edit().putString("password", "")
												.commit();
										Intent intent = new Intent();
										intent.setClass(MainPage.this,
												LoginPage.class);
										MainPage.this.startActivity(intent);

									}
								}
							})
					.setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
								}
							}).show();

		}

	}

	class ItemClickListener implements OnItemClickListener {

		public void onItemClick(AdapterView<?> arg0,// The AdapterView where the
													// click happened
				View arg1,// The view within the AdapterView that was clicked
				int arg2,// The position of the view in the adapter
				long arg3// The row id of the item that was clicked
		) {

			@SuppressWarnings("unchecked")
			HashMap<String, Object> item = (HashMap<String, Object>) arg0
					.getItemAtPosition(arg2);
			Intent intent = new Intent();
			if (item.get("ItemText").equals(
					getResources().getString(R.string.recommend))) {
				intent.setClass(MainPage.this, RecommendActivity.class);
			}
			if (item.get("ItemText").equals(
					getResources().getString(R.string.friendtrends))) {
				intent.setClass(MainPage.this, FriendsTrendsAuto.class);
			}
			if (item.get("ItemText").equals(
					getResources().getString(R.string.hottopic))) {
				intent.setClass(MainPage.this, Hottopic.class);
			}
			if (item.get("ItemText").equals(
					getResources().getString(R.string.messagecenter))) {

			}
			if (item.get("ItemText").equals(
					getResources().getString(R.string.myrecommend))) {
				intent.setClass(MainPage.this, MyRecommend.class);
				// 创建SharedPreferences
				SharedPreferences sp = getSharedPreferences("UserInfo",
						Context.MODE_WORLD_WRITEABLE
								| Context.MODE_WORLD_READABLE);
				// 获取数据
				String user = sp.getString("username", "defName");
				Bundle bundle = new Bundle();
				bundle.putString("item", user);
				bundle.putString("type", "");
				intent.putExtras(bundle);
			}
			if (item.get("ItemText").equals(
					getResources().getString(R.string.mytrends))) {
				intent.setClass(MainPage.this, UserTrendsManual.class);
				// 创建SharedPreferences
				SharedPreferences sp = getSharedPreferences("UserInfo",
						Context.MODE_WORLD_WRITEABLE
								| Context.MODE_WORLD_READABLE);
				// 获取数据
				String user = sp.getString("username", "defName");
				Bundle bundle = new Bundle();
				bundle.putString("item", "我");
				bundle.putString("type", "");
				intent.putExtras(bundle);
			}
			MainPage.this.startActivity(intent);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			new AlertDialog.Builder(MainPage.this)
					.setTitle(R.string.systeminfo)
					.setMessage(R.string.is_sure_cancel)
					.setPositiveButton(R.string.OK,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									Intent intent = new Intent();
									intent.setClass(MainPage.this,
											FirstPage.class);
									intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
									intent.putExtra("close", "close");
									startActivity(intent);
								}
							}).setNegativeButton(R.string.cancel, null).show();
		}
		return super.onKeyDown(keyCode, event);
	}
}